<?php


namespace app\common\traits;
use think\facade\Db;

/**
 * 动作日志记录
 * Class ActionRecordTrait
 * @package app\common\traits
 * @author Mr.Li
 * @create 2021-06-03 13:51
 */
trait ActionRecordTrait
{


    /**
     * @var string[] 模块
     */
    static $module = ['shop','crm','admin'];


    /**
     * 添加日志记录
     * @param $userId 用户id
     * @param $types  类型
     * @param $actionId 操作id
     * @param $content  内容
     * @param $module  模块
     * @param $controller  控制器
     * @return int|string
     * @author Mr.Li
     * @create 2021-06-03 14:16
     */
    public function addActionRecord($userId = 0,$types = '',$actionId = 0,$content = '',$module = '',$controller = '',$code = '')
    {
        $insertData['user_id']      = $userId;
        $insertData['types']        = $types;
        $insertData['action_id']    = $actionId;
        $insertData['content']      = $content;
        $insertData['module']       = $module;
        $insertData['controller']   = $controller;
        $insertData['create_time']  = time();
        $insertData['code']         = $code;
        return Db::name('admin_action_record')->insert($insertData);
    }

    /**
     * 获取日志记录
     * @param int $actionId
     * @param string $module
     * @param string $controller
     * @param int $userId
     * @param int $page
     * @param int $limit
     * @param string $order
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author Mr.Li
     * @create 2021-06-04 9:54
     */
    public function getActionRecordList($actionId = 0,$module = '',$controller = '',$userId = 0,$page = 1,$limit = 20,$order='id')
    {
        $where = $this->getWhereCondition($actionId,$module,$controller,$userId);
        if($where){
            // $logList = Db::name('admin_action_record')->where($where)->order($order,'desc')->limit(($page - 1) * $limit, $limit)->select()->toArray();
            $logList = Db::name('admin_action_record')->where($where)->order($order,'desc')
                ->when($limit != 0 ,function($query) use ($page, $limit){
                    $query->limit(($page - 1) * $limit, $limit);
                })
                ->select()->toArray();
            if($logList){
                $userIds = array_column($logList,'user_id');
                if($userIds){
                    $userInfo = Db::name('admin_user')->where('id','in',$userIds)->column('realname','id');
                }
                foreach($logList as &$v)
                {
                    if($v['content']){
                        $v['content'] = explode('.|.',$v['content']);
                    }
                    if($v['user_id']){
                        $v['realname'] = $userInfo[$v['user_id']] ?? '';
                    }
                    if($v['create_time']){
                        $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                    }
                }
                return $logList;
            }

            /*
            return Db::name('admin_action_record')->where($where)->order('id','desc')
                ->limit(($page - 1) * $limit, $limit)->select()->toArray();
            */
        }
        return [];

    }


    /**
     * 获取日志总数
     * @param int $actionId
     * @param string $module
     * @param string $controller
     * @param int $userId
     * @return int
     * @author Mr.Li
     * @create 2021-06-04 9:55
     */
    public function getActionRecordTotal($actionId = 0,$module = '',$controller = '',$userId = 0){
        $where =  $this->getWhereCondition($actionId, $module, $controller, $userId);
        return Db::name('admin_action_record')->where($where)->count();
    }


    /**
     * 获取日志记录
     * @param int $actionId
     * @param string $module
     * @param string $controller
     * @param int $userId
     * @param int $page
     * @param int $limit
     * @author Mr.Li
     * @create 2021-06-10 9:50
     */
    public function getActionRecordListByPage($actionId = 0,$module = '',$controller = '',$userId = 0,$page = 1,$limit = 20,$order = 'id')
    {
        $list = [];
        $dataCount = $this->getActionRecordTotal($actionId,$module,$controller,$userId);
        if($dataCount){
            $list = $this->getActionRecordList($actionId, $module, $controller, $userId,$page,$limit,$order);
        }
        return ['list' => $list,'dataCount' => $dataCount];
    }




    /**
     * 获取where查询条件
     * @param int $actionId
     * @param string $module
     * @param string $controller
     * @param int $userId
     * @return array
     * @author Mr.Li
     * @create 2021-06-04 9:58
     */
    public function getWhereCondition($actionId = 0,$module = '',$controller = '',$userId = 0)
    {
        $where = [['id','>',0]];
        if($actionId){
            $where[] = ['action_id','=',$actionId];
        }
        if($module){
            if(!in_array($module,self::$module)){
                return [];
            }
            $where[] = ['module','=',$module];
        }
        if($controller){
            $where[] = ['controller','=',$controller];
        }
        if($userId){
            $where[] = ['user_id','=',$userId];
        }
        return $where;
    }

}
